package com.softeem.music_sys.controller;

import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Music;
import com.softeem.music_sys.service.MusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 音乐控制器
 */
@Tag(name = "音乐接口", description = "音乐相关操作:添加、查询、修改、删除、播放等")
@RestController
@RequestMapping("/music")
public class MusicController {
    
    @Autowired
    private MusicService musicService;

    @Operation(summary = "添加音乐", description = "添加新音乐信息")
    @PostMapping
    public Result add(@RequestBody Music music) {
        return musicService.addMusic(music);
    }

    @Operation(summary = "获取音乐信息", description = "根据ID获取音乐详细信息")
    @GetMapping("/{id}")
    public Result getMusicById(@PathVariable Integer id) {
        return musicService.getMusicById(id);
    }

    @Operation(summary = "更新音乐信息", description = "更新音乐信息")
    @PutMapping
    public Result update(@RequestBody Music music) {
        return musicService.updateMusic(music);
    }

    @Operation(summary = "删除音乐", description = "根据ID删除音乐")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return musicService.deleteMusic(id);
    }

    @Operation(summary = "获取音乐列表", description = "分页获取音乐列表")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer limit) {
        return musicService.getMusicList(page, limit);
    }

    @Operation(summary = "搜索音乐", description = "根据歌曲名搜索")
    @GetMapping("/search")
    public Result search(@RequestParam String name,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit) {
        return musicService.searchMusicController(name, page, limit);
    }

    @Operation(summary = "获取专辑的音乐", description = "根据专辑ID获取其所有音乐")
    @GetMapping("/album/{albumId}")
    public Result getByAlbum(@PathVariable Integer albumId,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer limit) {
        return musicService.getMusicsByAlbumController(albumId, page, limit);
    }

    @Operation(summary = "获取热门音乐", description = "根据播放次数获取热门音乐")
    @GetMapping("/hot")
    public Result getHotMusic(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit) {
        return musicService.getHotMusicController(page, limit);
    }

    @Operation(summary = "获取最新音乐", description = "根据发行时间获取最新音乐")
    @GetMapping("/new")
    public Result getNewMusic(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit) {
        return musicService.getNewMusicController(page, limit);
    }

    @Operation(summary = "播放音乐", description = "播放音乐，播放次数+1")
    @PutMapping("/play/{id}")
    public Result playMusic(@PathVariable Integer id) {
        return musicService.playMusicController(id);
    }

    @Operation(summary = "修改音乐状态", description = "上架/下架音乐")
    @Parameters({
            @Parameter(name = "id", description = "音乐ID"),
            @Parameter(name = "status", description = "状态 1-上架 0-下架")
    })
    @PutMapping("/status/{id}")
    public Result updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        return musicService.updateStatusController(id, status);
    }
}